package com.rp.system.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.rp.common.utils.RpUtil;
import com.rp.log.annotation.RpLog;
import com.rp.system.dao.ButtonDao;
import com.rp.system.dao.ModuleDao;
import com.rp.system.dao.ResourceDao;
import com.rp.system.po.Button;
import com.rp.system.po.Module;
import com.rp.system.po.Resource;

@Service
@Transactional
public class ModuleService {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ModuleDao moduleDao;
	@Autowired
	private ResourceDao resourceDao;
	@Autowired
	private ButtonDao buttonDao;
	
	/**
	 * 添加模块
	 * @param module
	 */
	@RpLog(desc="添加模块")
	public void addModule(Module module){
		module.setCreateDate(RpUtil.getCurrentDatetime());
		moduleDao.save(module);
		try {
			buttonDao.addDefaultButton(module.getAlias());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新模块
	 * @param module
	 */
	@RpLog(desc="更新模块")
	public void updateModule(Module module){
		
		List<Resource> resc = resourceDao.find("from Resource where moduleId = ?", module.getId());
		for (Resource res : resc) {
			res.setModuleName(module.getName());
		}
		
		Record r = Db.use("rpDb").findById("rp_system_modules", module.getId());
		List<Button> btns = buttonDao.find("from Button where moduleAlias = ?", r.get("alias"));
		for (Button btn : btns) {
			btn.setModuleAlias(module.getAlias());
		}
		
		module.setUpdateDate(RpUtil.getCurrentDatetime());
		moduleDao.update(module);
	}
	
	/**
	 * 删除模块
	 * @param moduleId
	 */
	@RpLog(desc="删除模块")
	public void deleteModule(String moduleId){
		
		Module dbModule = moduleDao.get(moduleId);
		
		List<Resource> rescs = resourceDao.find("from Resource where moduleId = ?", moduleId);
		for (Resource resc : rescs) {
			resc.setModuleId(null);
			resc.setModuleName(null);
		}
		
		List<Button> btns = buttonDao.find("from Button where moduleAlias = ?", dbModule.getAlias());
		for (Button btn : btns) {
			buttonDao.delete(btn);
		}
		
		moduleDao.delete(dbModule);
	}
	
	/**
	 * 添加模块
	 * @param name
	 * @param parentId
	 * @return
	 */
	@Deprecated
	@RpLog(desc="新增模块")
	public String addModule(String name, String parentId){
		String sql = "insert into RP_SYSTEM_MODULES(GUID, NAME, PARENT_ID, CREATEDATE) values(?,?,?,?)";
		String guid = RpUtil.createGuid();
		Object[] params = new Object[]{guid, name, parentId, new Date()};
		jdbcTemplate.update(sql, params);
		return guid;
	}
	
	/**
	 * 移动模块
	 * @param ids
	 * @param targetId
	 * @throws Exception 
	 */
	@RpLog(desc="移动模块")
	public void moveModules(String[] ids, String targetId) throws Exception{
		Module targetModule = moduleDao.get(targetId);
		for (String id : ids) {
			Module m = moduleDao.get(id);
			if(m.getId().equals(targetModule.getParentId())){
				throw new Exception("目标节点不能是被移动节点的子节点");
			}else{
				m.setParentId(targetId);
			}
		}
	}
	
	/**
	 * 移动模块至根节点
	 * @param ids
	 */
	@RpLog(desc="移动模块至根节点")
	public void moveModulesToRoot(String[] ids){
		for (String id : ids) {
			Module m = moduleDao.get(id);
			m.setParentId(null);
		}
	}
	
	
	public ModuleDao getModuleDao() {
		return moduleDao;
	}
	
}
